package com.kde.crisisdepalabras;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

import com.facebook.Session;
import com.facebook.android.Util;
import com.facebook.model.GraphUser;
import com.kde.crisisdepalabras.Model.Combat;
import com.kde.crisisdepalabras.Model.Player;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;

public class UserMainActivity extends Activity implements TaskComplete
{
	  private ArrayList<Combat> combats = new ArrayList<Combat>();
	  private GlobalClass globalVariable;
	  
	  @Override
	    protected void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_usermain);

	        this.globalVariable = (GlobalClass)getApplicationContext();
	        
	        TextView userTextView = (TextView)findViewById(R.id.user_name);
            userTextView.setText(this.globalVariable.getName() + this.globalVariable.getSurname());	                               
            
	        //Traigo los duelos.	    
	        ServiceTask service = new ServiceTask();
			service.setTaskComplete(this);
			service.execute("http://appeventscomar.fatcow.com/wsPreguntas/GetDuelo.php?Player=" +  this.globalVariable.getId());
	    }
	  
		@Override
		public boolean onCreateOptionsMenu(Menu menu) 
		{
			this.getMenuInflater().inflate(R.menu.main, menu);
			return true;
		}
		
		@Override
		public void onBackPressed() 
		{
			this.setResult(0);
			this.finish();
		}
		
		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event)
		{
		    if ( keyCode == KeyEvent.KEYCODE_MENU )
		    {
		    	this.openOptionsMenu();
		        return true;
		    }
		    return super.onKeyDown(keyCode, event);
		}
		
		@Override
	    public boolean onOptionsItemSelected(MenuItem item)
	    {
			
		     switch(item.getItemId())
		     {
		     	case R.id.logout:	
		     		Session session = Session.getActiveSession();
		     		session.closeAndClearTokenInformation();
		     		
		     		Intent i = new Intent(this, MainActivity.class);
		     		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		     		startActivity(i);
		     				     		
		     		return true;
		     	
		     	case R.id.about:	

		     		return true;
	     }
	     return super.onOptionsItemSelected(item);

	    }

	   	 public void PlayersSelection(View view) 
	   	 {
	   		 Intent newCombat = new Intent(UserMainActivity.this, NewCombatActivity.class);
	   	  	 startActivityForResult(newCombat,0); 
	   	 }

		 public void Ejecutar(View view) 
		 {
			 
			 Intent quest= null;
			 /*
			 switch (view.getId()) 
			 {		 
				 case R.id.btnCulltura:		 
			        quest = new Intent(this, QuestionActivity.class);	
			        quest.putExtra("value1", "BotonCultura");
			        break;
				 case R.id.btnDeportes:
					quest = new Intent(this, QuestionActivity.class);	
					quest.putExtra("value1", "BotonDeportes");
					break;
			 }
			 */
			 
			 try 
			 {
				 if (quest != null)
				 {
					 this.startActivity(quest);
				 }
			 }
			 catch (Exception exception)
			 {
				 Log.i(exception.getMessage(), "Logged out...");
			 }
			 
		 }

		@Override
		public void onComplete(JSONArray result) 
		{
			try 
			{
				for(int i=0;i<result.length();i++)
				{
					JSONObject json_data = result.getJSONObject(i);

					Player player = new Player(json_data.getString("Player1"), json_data.getString("Player1"), json_data.getString("Player1"), "");						 
				   	Combat combat = new Combat(json_data.getString("idDuelo"), player, 1, 5, 6);
					this.combats.add(combat);		
				}
				
				ListView listViewCombats = (ListView)findViewById(R.id.listViewCombats);
	            CombatAdapter listAdapter = new CombatAdapter(this, R.layout.combat, combats);
		        listViewCombats.setAdapter(listAdapter);	
			}	        
			catch (Exception e) 
			{
				// TODO: handle exception
			}
			
		}
}
